package com.softstao.guoyu.ui.activity.sale;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.global.UserManager;
import com.softstao.guoyu.model.sale.OrderDetail;
import com.softstao.guoyu.model.sale.OrderGoods;
import com.softstao.guoyu.model.sale.PayCondition;
import com.softstao.guoyu.model.sale.ShippingCondition;
import com.softstao.guoyu.mvp.interactor.sale.OrderInteractor;
import com.softstao.guoyu.mvp.presenter.sale.OrderPresenter;
import com.softstao.guoyu.mvp.viewer.sale.OrderDetailViewer;
import com.softstao.guoyu.mvp.viewer.sale.OrderViewer;
import com.softstao.guoyu.ui.adapter.OrderGoodsAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewBaseAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewHolder;
import com.softstao.guoyu.widget.HintDialog;
import com.softstao.softstaolibrary.library.activity.ImagePagerActivity;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.CustomScrollerView;
import com.softstao.softstaolibrary.library.widget.FullyGridLayoutManager;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;
import com.softstao.softstaolibrary.library.widget.LZToast;
import com.softstao.softstaolibrary.library.widget.MarginDecoration;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/6/19.
 */

public class OrderDetailActivity extends BaseActivity implements OrderDetailViewer, OrderViewer {

    @AIPresenter(presenter = OrderPresenter.class, interactor = OrderInteractor.class)
    OrderPresenter presenter;

    @BindView(R.id.btn4)
    TextView btn4;
    @BindView(R.id.btn3)
    TextView btn3;
    @BindView(R.id.btn2)
    TextView btn2;
    @BindView(R.id.btn1)
    TextView btn1;
    @BindView(R.id.bottom)
    LinearLayout bottom;
    @BindView(R.id.order_sn)
    TextView orderSn;
    @BindView(R.id.agent)
    TextView agent;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.status)
    TextView status;
    @BindView(R.id.list_view)
    RecyclerView listView;
    @BindView(R.id.total_price)
    TextView totalPrice;
    @BindView(R.id.orderer)
    TextView orderer;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.pay_method)
    TextView payMethod;
    @BindView(R.id.pay_price)
    TextView payPrice;
    @BindView(R.id.pay_memo)
    TextView payMemo;
    @BindView(R.id.scroll_view)
    CustomScrollerView scrollView;
    @BindView(R.id.img_view)
    RecyclerView imgView;
    @BindView(R.id.loading)
    RelativeLayout loading;
    @BindView(R.id.shipping_view)
    RecyclerView shippingView;
    @BindView(R.id.contact)
    TextView contact;
    @BindView(R.id.contact_orderer)
    TextView contactOrderer;
//    @BindView(R.id.memo)
//    TextView memo;
    @BindView(R.id.cancel_reason)
    TextView cancelReason;
    @BindView(R.id.cancel_view)
    LinearLayout cancelView;
    private String order_title;
    private String order_code;
    private OrderDetail orderDetail;
    private List<OrderGoods> goodsList = new ArrayList<>();
    private List<String> imgList = new ArrayList<>();
    private List<OrderDetail.LogisticsInfoBean> shippingList = new ArrayList<>();
    private RecycleViewBaseAdapter<OrderDetail.LogisticsInfoBean> shippingAdapter;
    private OrderGoodsAdapter adapter;
    private RecycleViewBaseAdapter<String> imgAdapter;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static int CANCEL = 1;
    private final static int REJECT = 2;
    private final static int PAY = 3;
    private final static int SHIPPING = 4;
    private String cancel_reason;
    private String reject_reason;
    private PayCondition payCondition = new PayCondition();
    private ShippingCondition shippingCondition = new ShippingCondition();

    @Override
    public int _ContentView() {
        return R.layout.activity_order_detail;
    }

    @Override
    public void initView() {
        order_title = getIntent().getStringExtra("order_title");
        order_code = getIntent().getStringExtra("order_code");
        if (order_title != null && !order_title.equals("")) {
            switch (order_title) {
                case "from_order":
                    initTitle("下级采购单详情");
                    contact.setVisibility(View.GONE);
                    break;
                case "to_order":
                    initTitle("我发起的采购单详情");
                    contactOrderer.setVisibility(View.GONE);
                    break;
                case "transfer":
                    initTitle("我的转采购单详情");
                    break;
            }
        }

        adapter = new OrderGoodsAdapter(goodsList);
        listView.setAdapter(adapter);
        listView.setLayoutManager(new FullyLinearLayoutManager(this));

        imgAdapter = new RecycleViewBaseAdapter<String>(imgList, R.layout.layout_img) {
            @Override
            public void convert(RecycleViewHolder holder, String s) {
                Glide.with(context).load(s).into((ImageView) holder.getView(R.id.img));
                holder.getView(R.id.img).setOnClickListener(v -> imageBrower(holder.getAdapterPosition(), (ArrayList<String>) imgList));
            }
        };
        imgView.setAdapter(imgAdapter);
        imgView.setLayoutManager(new FullyGridLayoutManager(context, 3));
        imgView.addItemDecoration(new MarginDecoration(this));

        shippingAdapter = new RecycleViewBaseAdapter<OrderDetail.LogisticsInfoBean>(shippingList, R.layout.layout_shipping_item) {
            @Override
            public void convert(RecycleViewHolder holder, OrderDetail.LogisticsInfoBean logisticsInfoBean) {
                RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                holder.itemView.setLayoutParams(layoutParams);
                String company = logisticsInfoBean.getMode() == null ? "—" : logisticsInfoBean.getMode();
                String num = logisticsInfoBean.getLogisticsCode() == null ? "—" : logisticsInfoBean.getLogisticsCode();
                holder.setText(R.id.shipping_company, "快递公司：" + company)
                        .setText(R.id.shipping_num, "物流单号：" + num);
                holder.getView(R.id.copy).setOnClickListener(v -> {
                    ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                    cm.setText(num);
                    LZToast.getInstance(context).showToast("复制成功");
                });
            }
        };
        shippingView.setAdapter(shippingAdapter);
        shippingView.setLayoutManager(new FullyLinearLayoutManager(this));

        presenter = new OrderPresenter();
        presenter.setInteractor(new OrderInteractor());
        presenter.setViewer(this);
        findDetail();
    }

    protected void imageBrower(int position, ArrayList<String> urls2) {
        Intent intent = new Intent(this, ImagePagerActivity.class);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
        intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
        startActivity(intent);
    }

    private void initData() {
        orderSn.setText("订单编号：" + orderDetail.getOrderCode());
        agent.setText("商家：" + orderDetail.getAgentName());
        time.setText("时间：" + format.format(new Date(orderDetail.getOrderDate())));
        totalPrice.setText(getResources().getString(R.string.rmb) + LZUtils.priceFormat(orderDetail.getTotal()));
//        String buy_memo = orderDetail.getRemark() != null ? orderDetail.getRemark() : "—";
//        memo.setText(buy_memo);
        if (TextUtils.isEmpty(orderDetail.getCancelReason()) == false) {
            cancelView.setVisibility(View.VISIBLE);
            cancelReason.setText(orderDetail.getCancelReason());
        } else {
            cancelView.setVisibility(View.GONE);
        }
        if (orderDetail.getConsigneeInfo() != null) {
            orderer.setText("收货人：" + orderDetail.getConsigneeInfo().getName());
            address.setText(orderDetail.getConsigneeInfo().getAddress());
        }
        if (orderDetail.getLogisticsInfo() != null && orderDetail.getLogisticsInfo().size() != 0) {
            shippingList.clear();
            shippingList.addAll(orderDetail.getLogisticsInfo());
            shippingAdapter.notifyDataSetChanged();
        }
        if (orderDetail.getPayInfo() != null) {
            String method = orderDetail.getPayInfo().getWay() != null ? orderDetail.getPayInfo().getWay() : "—";
            String amount = orderDetail.getPayInfo().getPayAmount() != 0 ? LZUtils.priceFormat(orderDetail.getPayInfo().getPayAmount()) : "—";
            String memo = orderDetail.getPayInfo().getPayRemark() != null ? orderDetail.getPayInfo().getPayRemark() : "—";
            payMethod.setText("付款方式：" + method);
            payPrice.setText("付款金额：" + amount);
            payMemo.setText(memo);
            if (orderDetail.getPayInfo().getPayImg() != null && orderDetail.getPayInfo().getPayImg().size() != 0) {
                imgView.setVisibility(View.VISIBLE);
                imgList.clear();
                imgList.addAll(orderDetail.getPayInfo().getPayImg());
                imgAdapter.notifyDataSetChanged();
            } else {
                imgView.setVisibility(View.GONE);
            }
        }
    }

    @OnClick({R.id.contact, R.id.contact_orderer, R.id.copy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.contact:
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + orderDetail.getAgentMobile());
                intent.setData(data);
                startActivity(intent);
                break;
            case R.id.contact_orderer:
                Intent orderIntent = new Intent(Intent.ACTION_DIAL);
                Uri data1 = Uri.parse("tel:" + orderDetail.getConsigneeInfo().getMobile());
                orderIntent.setData(data1);
                startActivity(orderIntent);
                break;
            case R.id.copy:
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(orderDetail.getOrderCode());
                LZToast.getInstance(context).showToast("复制成功");
                break;
        }
    }

    @Override
    public void findDetail() {
        loading.setVisibility(View.VISIBLE);
        presenter.getOrderDetail(SharePreferenceManager.getInstance().getAgentId(), order_code);
    }

    @Override
    public void getOrderDetail(OrderDetail orderDetail) {
        loading.setVisibility(View.GONE);
        if (orderDetail == null) {
            LZToast.getInstance(this).showToast("订单不存在！");
            finish();
        } else {
            this.orderDetail = orderDetail;
            goodsList.clear();
            goodsList.addAll(orderDetail.getOrderProductList());
            adapter.notifyDataSetChanged();
            adapter.setStatus(orderDetail.getState());
            initData();
            changeStatus();
        }
    }

    private void changeStatus() {
        switch (orderDetail.getState()) {
            case OrderDetail.UNCONFIRM:
                status.setText(orderDetail.getStateStr());
                switch (order_title) {
                    case "from_order":
                        btn1.setVisibility(View.VISIBLE);
                        btn1.setText("确认订单");
                        btn1.setOnClickListener(v -> {
                            HintDialog dialog = new HintDialog(context, R.style.MyDialog);
                            dialog.getWindow().setLayout(getScreenWidth() - LZUtils.dipToPix(context, 30) * 2, ViewGroup.LayoutParams.WRAP_CONTENT);
                            dialog.getIcon().setVisibility(View.GONE);
                            dialog.setTitle("确认订单");
                            dialog.setContentVisible();
                            dialog.setContent("确认接收该订单！");
                            dialog.setCancelVisible();
                            dialog.setConfirm("确认");
                            dialog.getConfirm().setOnClickListener(v1 -> {
                                orderConfirm();
                                dialog.dismiss();
                            });
                            dialog.show();
                        });
                        btn2.setVisibility(View.VISIBLE);
                        btn2.setText("取消订单");
                        btn2.setOnClickListener(v -> startActivityForResult(new Intent(context, OrderCancelActivity.class).putExtra("type", "cancel"), CANCEL));
                        btn3.setVisibility(View.GONE);
                        btn4.setVisibility(View.GONE);
                        break;
                    case "transfer":
                    case "to_order":
                        btn1.setVisibility(View.VISIBLE);
                        btn1.setText("取消订单");
                        btn1.setOnClickListener(v -> {
                            cancel_reason = "";
                            new AlertDialog.Builder(context)
                                    .setMessage("确定取消订单吗？")
                                    .setPositiveButton("确定", (dialog1, which) -> {
                                        dialog1.dismiss();
                                        orderCancel();
                                    })
                                    .setNegativeButton("取消", (dialog1, which) -> dialog1.dismiss())
                                    .create().show();
                        });
                        btn2.setVisibility(View.GONE);
                        btn3.setVisibility(View.GONE);
                        btn4.setVisibility(View.GONE);
                        break;
                }
                break;
            case OrderDetail.UNPAY:
                if (orderDetail.getPayInfo() != null) {
                    switch (order_title) {
                        case "from_order":
                            status.setText(orderDetail.getStateStr());
                            if (orderDetail.getIsPay() == 1) {
                                btn1.setVisibility(View.GONE);
                                btn1.setText("确认并发货");
                                btn1.setOnClickListener(v -> {
                                    HintDialog dialog = new HintDialog(context, R.style.MyDialog);
                                    dialog.getWindow().setLayout(getScreenWidth() - LZUtils.dipToPix(context, 30) * 2, ViewGroup.LayoutParams.WRAP_CONTENT);
                                    dialog.getIcon().setVisibility(View.GONE);
                                    dialog.setTitle("确认收款并发货");
                                    dialog.setContentVisible();
                                    dialog.setContent("确认收款并准备自己发货吗？\n发货后请到“待发货”订单填写快递信息。");
                                    dialog.setCancelVisible();
                                    dialog.setConfirm("确认");
                                    dialog.getConfirm().setOnClickListener(v1 -> {
                                        orderConfirmIncome();
                                        dialog.dismiss();
                                    });
                                    dialog.show();
                                });
                                btn2.setVisibility(View.VISIBLE);
                                btn2.setText("确认并上移");
                                btn2.setOnClickListener(v -> {
                                    HintDialog dialog = new HintDialog(context, R.style.MyDialog);
                                    dialog.getWindow().setLayout(getScreenWidth() - LZUtils.dipToPix(context, 30) * 2, ViewGroup.LayoutParams.WRAP_CONTENT);
                                    dialog.getIcon().setVisibility(View.GONE);
                                    dialog.setTitle("确认收款并上移订单");
                                    dialog.setContentVisible();
                                    dialog.setContent("确认上移订单吗？\n上移的订单请到“我的转采购单”中查看。");
                                    dialog.setCancelVisible();
                                    dialog.setConfirm("确认");
                                    dialog.getConfirm().setOnClickListener(v1 -> {
                                        orderTransfer();
                                        dialog.dismiss();
                                    });
                                    dialog.show();
                                });
                                btn3.setVisibility(View.VISIBLE);
                                btn3.setText("驳回");
                                btn3.setOnClickListener(v -> startActivityForResult(new Intent(context, OrderCancelActivity.class).putExtra("type", "reject"), REJECT));
                                btn4.setVisibility(View.VISIBLE);
                                btn4.setText("取消订单");
                                btn4.setOnClickListener(v -> startActivityForResult(new Intent(context, OrderCancelActivity.class).putExtra("type", "cancel"), CANCEL));
                            } else if (orderDetail.getIsPay() == 0) {
                                btn1.setVisibility(View.VISIBLE);
                                btn1.setText("取消订单");
                                btn1.setOnClickListener(v -> startActivityForResult(new Intent(context, OrderCancelActivity.class).putExtra("type", "cancel"), CANCEL));
                                btn2.setVisibility(View.GONE);
                                btn3.setVisibility(View.GONE);
                                btn4.setVisibility(View.GONE);
                            } else if (orderDetail.getIsPay() == 2) {
                                btn1.setVisibility(View.VISIBLE);
                                btn1.setText("取消订单");
                                btn1.setOnClickListener(v -> startActivityForResult(new Intent(context, OrderCancelActivity.class).putExtra("type", "cancel"), CANCEL));
                                btn2.setVisibility(View.GONE);
                                btn3.setVisibility(View.GONE);
                                btn4.setVisibility(View.GONE);
                            }
                            break;
                        case "transfer":
                        case "to_order":
                            status.setText(orderDetail.getStateStr());
                            if (UserManager.getInstance().getUser().getLevel() != 5) {
                                if (orderDetail.getIsPay() == 2) {
                                    btn1.setVisibility(View.VISIBLE);
                                    btn1.setText("重新付款");
                                    btn1.setOnClickListener(v -> {
                                        Intent intent = new Intent(context, OrderPayActivity.class);
                                        intent.putExtra("order_detail", orderDetail);
                                        intent.putExtra("status", "detail");
                                        startActivityForResult(intent, PAY);
                                    });
                                    btn2.setVisibility(View.VISIBLE);
                                    btn2.setText("取消订单");
                                    btn2.setOnClickListener(v -> {
                                        cancel_reason = "";
                                        new AlertDialog.Builder(context)
                                                .setMessage("确定取消订单吗？")
                                                .setPositiveButton("确定", (dialog1, which) -> {
                                                    dialog1.dismiss();
                                                    orderCancel();
                                                })
                                                .setNegativeButton("取消", (dialog1, which) -> dialog1.dismiss())
                                                .create().show();
                                    });
                                    btn3.setVisibility(View.GONE);
                                    btn4.setVisibility(View.GONE);
                                } else if (orderDetail.getIsPay() == 0) {
                                    btn1.setVisibility(View.VISIBLE);
                                    btn1.setText("付款");
                                    btn1.setOnClickListener(v -> {
                                        Intent intent = new Intent(context, OrderPayActivity.class);
                                        intent.putExtra("order_detail", orderDetail);
                                        intent.putExtra("status", "detail");
                                        startActivityForResult(intent, PAY);
                                    });
                                    btn2.setVisibility(View.VISIBLE);
                                    btn2.setText("取消订单");
                                    btn2.setOnClickListener(v -> {
                                        cancel_reason = "";
                                        new AlertDialog.Builder(context)
                                                .setMessage("确定取消订单吗？")
                                                .setPositiveButton("确定", (dialog1, which) -> {
                                                    dialog1.dismiss();
                                                    orderCancel();
                                                })
                                                .setNegativeButton("取消", (dialog1, which) -> dialog1.dismiss())
                                                .create().show();
                                    });
                                    btn3.setVisibility(View.GONE);
                                    btn4.setVisibility(View.GONE);
                                } else if (orderDetail.getIsPay() == 1) {
                                    bottom.setVisibility(View.GONE);
                                }
                            } else {
                                btn1.setVisibility(View.VISIBLE);
                                btn1.setText("取消订单");
                                btn1.setOnClickListener(v -> {
                                    cancel_reason = "";
                                    new AlertDialog.Builder(context)
                                            .setMessage("确定取消订单吗？")
                                            .setPositiveButton("确定", (dialog1, which) -> {
                                                dialog1.dismiss();
                                                orderCancel();
                                            })
                                            .setNegativeButton("取消", (dialog1, which) -> dialog1.dismiss())
                                            .create().show();
                                });
                                btn2.setVisibility(View.GONE);
                                btn3.setVisibility(View.GONE);
                                btn4.setVisibility(View.GONE);
                            }
                            break;
                    }
                }
                break;
            case OrderDetail.PAYED:
                status.setText(orderDetail.getStateStr());
                switch (order_title) {
                    case "from_order":
                        btn1.setVisibility(View.VISIBLE);
                        btn1.setText("填写快递信息");
                        btn1.setOnClickListener(v -> {
                            Intent intent = new Intent(context, OrderShippingActivity.class);
                            intent.putExtra("order_detail", orderDetail);
                            intent.putExtra("type", "detail");
                            startActivityForResult(intent, SHIPPING);
                        });
                        btn2.setVisibility(View.VISIBLE);
                        btn2.setText("取消订单");
                        btn2.setOnClickListener(v -> startActivityForResult(new Intent(context, OrderCancelActivity.class).putExtra("type", "cancel"), CANCEL));
                        btn3.setVisibility(View.GONE);
                        btn4.setVisibility(View.GONE);
                        break;
                    case "transfer":
                    case "to_order":
                        bottom.setVisibility(View.GONE);
                        break;
                }
                break;
            case OrderDetail.SHIPPED:
                status.setText(orderDetail.getStateStr());
                switch (order_title) {
                    case "from_order":
                        bottom.setVisibility(View.GONE);
                        break;
                    case "to_order":
                        btn1.setVisibility(View.VISIBLE);
                        btn1.setText("确认收货");
                        btn1.setOnClickListener(v -> {
                            HintDialog dialog = new HintDialog(context, R.style.MyDialog);
                            dialog.getWindow().setLayout(getScreenWidth() - LZUtils.dipToPix(context, 30) * 2, ViewGroup.LayoutParams.WRAP_CONTENT);
                            dialog.getIcon().setVisibility(View.GONE);
                            dialog.setTitle("确认收货");
                            dialog.setContentVisible();
                            dialog.setContent("确认收货吗？");
                            dialog.setCancelVisible();
                            dialog.setConfirm("确认");
                            dialog.getConfirm().setOnClickListener(v1 -> {
                                orderReceive();
                                dialog.dismiss();
                            });
                            dialog.show();
                        });
                        btn2.setVisibility(View.GONE);
                        btn3.setVisibility(View.GONE);
                        btn4.setVisibility(View.GONE);
                        break;
                    case "transfer":
                        bottom.setVisibility(View.GONE);
                        break;
                }
                break;
            case OrderDetail.COMPLETED:
                status.setText(orderDetail.getStateStr());
                bottom.setVisibility(View.GONE);
                break;
            case OrderDetail.CANCEL:
                status.setText(orderDetail.getStateStr());
                switch (order_title) {
                    case "from_order":
                        bottom.setVisibility(View.GONE);
                        break;
                    case "to_order":
                        btn1.setVisibility(View.VISIBLE);
                        btn1.setText("重新提交");
                        btn1.setOnClickListener(v ->
                                new AlertDialog.Builder(context)
                                        .setMessage("确定重新提交订单？")
                                        .setPositiveButton("确定", (dialog1, which) -> {
                                            orderResubmit();
                                            dialog1.dismiss();
                                        }).setNegativeButton("取消", (dialog1, which) -> dialog1.dismiss())
                                        .create().show()
                        );
                        btn2.setVisibility(View.VISIBLE);
                        btn2.setText("删除订单");
                        btn2.setOnClickListener(v ->
                                new AlertDialog.Builder(context)
                                        .setMessage("确定删除此订单？")
                                        .setPositiveButton("确定", (dialog1, which) -> {
                                            orderDelete();
                                            dialog1.dismiss();
                                        }).setNegativeButton("取消", (dialog1, which) -> dialog1.dismiss())
                                        .create().show()
                        );
                        btn3.setVisibility(View.GONE);
                        btn4.setVisibility(View.GONE);
                        break;
                    case "transfer":
                        btn1.setVisibility(View.VISIBLE);
                        btn1.setText("重新提交");
                        btn1.setOnClickListener(v ->
                                new AlertDialog.Builder(context)
                                        .setMessage("确定重新提交订单？")
                                        .setPositiveButton("确定", (dialog1, which) -> {
                                            orderResubmit();
                                            dialog1.dismiss();
                                        }).setNegativeButton("取消", (dialog1, which) -> dialog1.dismiss())
                                        .create().show()
                        );
                        btn2.setVisibility(View.VISIBLE);
                        btn2.setText("撤销转采购");
                        btn2.setOnClickListener(v -> {
                            new AlertDialog.Builder(context)
                                    .setMessage("确定撤销转采购？")
                                    .setPositiveButton("确定", (dialog1, which) -> {
                                        orderRevoke();
                                        dialog1.dismiss();
                                    }).setNegativeButton("取消", (dialog1, which) -> dialog1.dismiss())
                                    .create().show();
                        });
                        btn3.setVisibility(View.GONE);
                        btn4.setVisibility(View.GONE);
                        break;
                }
                break;
        }
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        loading.setVisibility(View.GONE);
    }

    @Override
    public void orderCancel() {
        loading.setVisibility(View.VISIBLE);
        presenter.orderCancel(SharePreferenceManager.getInstance().getAgentId(), order_code, cancel_reason);
    }

    @Override
    public void orderConfirmIncome() {
        loading.setVisibility(View.VISIBLE);
        presenter.orderConfirmIncome(SharePreferenceManager.getInstance().getAgentId(), order_code);
    }

    @Override
    public void orderRejectIncome() {
        loading.setVisibility(View.VISIBLE);
        presenter.orderRejectIncome(SharePreferenceManager.getInstance().getAgentId(), order_code, reject_reason);
    }

    @Override
    public void orderSubmitShipping() {
        loading.setVisibility(View.VISIBLE);
        presenter.orderSubmitShipping(shippingCondition);
    }

    @Override
    public void orderPay() {
        loading.setVisibility(View.VISIBLE);
        presenter.orderPay(payCondition);
    }

    @Override
    public void orderConfirm() {
        loading.setVisibility(View.VISIBLE);
        presenter.orderConfirm(SharePreferenceManager.getInstance().getAgentId(), order_code);
    }

    @Override
    public void orderResubmit() {
        loading.setVisibility(View.VISIBLE);
        presenter.orderResubmit(SharePreferenceManager.getInstance().getAgentId(), order_code);
    }

    @Override
    public void orderTransfer() {
        loading.setVisibility(View.VISIBLE);
        presenter.orderTransfer(SharePreferenceManager.getInstance().getAgentId(), order_code);
    }

    @Override
    public void orderReceive() {
        loading.setVisibility(View.VISIBLE);
        presenter.orderReceive(SharePreferenceManager.getInstance().getAgentId(), order_code);
    }

    @Override
    public void orderRevoke() {
        loading.setVisibility(View.VISIBLE);
        presenter.orderRevoke(SharePreferenceManager.getInstance().getAgentId(), order_code);
    }

    @Override
    public void orderDelete() {
        loading.setVisibility(View.VISIBLE);
        presenter.orderDelete(SharePreferenceManager.getInstance().getAgentId(), order_code);
    }

    @Override
    public void cancelResult(Object result) {
        loading.setVisibility(View.GONE);
        HintDialog dialog = new HintDialog(context, R.style.MyDialog);
        dialog.getWindow().setLayout(getScreenWidth() - LZUtils.dipToPix(context, 55) * 2, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setIcon(R.mipmap.hint_icon_success);
        dialog.setTitle("取消订单成功");
        dialog.setConfirm("确认");
        dialog.getConfirm().setOnClickListener(v -> dialog.dismiss());
        dialog.show();
        findDetail();
    }

    @Override
    public void confirmIncomeResult(Object result) {
        loading.setVisibility(View.GONE);
        LZToast.getInstance(context).showToast("确认成功");
        findDetail();
    }

    @Override
    public void rejectIncomeResult(Object result) {
        loading.setVisibility(View.GONE);
        LZToast.getInstance(context).showToast("驳回成功");
        findDetail();
    }

    @Override
    public void submitShippingResult(Object result) {
        loading.setVisibility(View.GONE);
        HintDialog dialog = new HintDialog(context, R.style.MyDialog);
        dialog.getWindow().setLayout(getScreenWidth() - LZUtils.dipToPix(context, 55) * 2, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setIcon(R.mipmap.hint_icon_success);
        dialog.setTitle("发货成功");
        dialog.setConfirm("确认");
        dialog.getConfirm().setOnClickListener(v -> dialog.dismiss());
        dialog.show();
        findDetail();
    }

    @Override
    public void payResult(Object result) {
        loading.setVisibility(View.GONE);
        HintDialog dialog = new HintDialog(context, R.style.MyDialog);
        dialog.getWindow().setLayout(getScreenWidth() - LZUtils.dipToPix(context, 55) * 2, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setIcon(R.mipmap.hint_icon_success);
        dialog.setTitle("付款成功");
        dialog.setContentVisible();
        dialog.setContent("请耐心等待发货!");
        dialog.setConfirm("确认");
        dialog.getConfirm().setOnClickListener(v -> dialog.dismiss());
        dialog.show();
        findDetail();
    }

    @Override
    public void confirmResult(Object result) {
        loading.setVisibility(View.GONE);
        LZToast.getInstance(context).showToast("确认成功");
        findDetail();
    }

    @Override
    public void reSubmitResult(Object result) {
        loading.setVisibility(View.GONE);
        LZToast.getInstance(context).showToast("提交成功");
        findDetail();
    }

    @Override
    public void transferResult(Object result) {
        loading.setVisibility(View.GONE);
        LZToast.getInstance(context).showToast("确认成功");
        findDetail();
        startActivity(new Intent(context, MyOrderListActivity.class).putExtra("orderState", ""));
        finish();
    }

    @Override
    public void receiveResult(Object result) {
        loading.setVisibility(View.GONE);
        LZToast.getInstance(context).showToast("确认收货成功");
        findDetail();
    }

    @Override
    public void revokeResult(Object result) {
        loading.setVisibility(View.GONE);
        LZToast.getInstance(context).showToast("撤销成功");
        finish();
    }

    @Override
    public void deleteResult(Object result) {
        loading.setVisibility(View.GONE);
        LZToast.getInstance(context).showToast("删除成功");
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CANCEL:
                    cancel_reason = data.getStringExtra("reason");
                    orderCancel();
                    break;
                case REJECT:
                    reject_reason = data.getStringExtra("reason");
                    orderRejectIncome();
                    break;
                case PAY:
                    payCondition = (PayCondition) data.getSerializableExtra("pay");
                    payCondition.setAgentId(SharePreferenceManager.getInstance().getAgentId());
                    payCondition.setOrderCode(order_code);
                    orderPay();
                    break;
                case SHIPPING:
                    shippingCondition = (ShippingCondition) data.getSerializableExtra("shipping");
                    shippingCondition.setAgentId(SharePreferenceManager.getInstance().getAgentId());
                    shippingCondition.setOrderCode(order_code);
                    orderSubmitShipping();
                    break;
            }
        }
    }
}
